local TeleportService = game:GetService("TeleportService")

local screenGui = script.Parent

local cfg_file = screenGui:WaitForChild("Config")
local item_pfb = cfg_file:WaitForChild("Item")
local tips = screenGui:WaitForChild("Tips")

local main_frame = screenGui:WaitForChild("MainFrame")
local main_frame_bg = main_frame:WaitForChild("BG")
local scroll = main_frame_bg:WaitForChild("ScrollingFrame")
local grid_layout = scroll:WaitForChild("UIGridLayout")
local main_frame_close = main_frame_bg:WaitForChild("CloseBtn")
local main_frame_title = main_frame_bg:WaitForChild("Title")
local main_cloae_window = main_frame:WaitForChild("CloseWindow")
local bottom = main_frame_bg:WaitForChild("Bottom")
local bottom_info = bottom:WaitForChild("Info")
-----------------------
local show_frame = screenGui:WaitForChild("ShowFrame")
local show_frame_bg = show_frame:WaitForChild("BG")
local sure_btn = show_frame_bg:WaitForChild("SureBtn")
local sure_btn_info = sure_btn:WaitForChild("Info")
local cancel_btn = show_frame_bg:WaitForChild("CancelBtn")
local cancel_btn_info = cancel_btn:WaitForChild("Info")
local show_icon = show_frame_bg:WaitForChild("Icon")
local show_des = show_frame_bg:WaitForChild("Des")
local show_title = show_frame_bg:WaitForChild("Title")
local show_cloae_window = show_frame:WaitForChild("CloseWindow")
local show_name = show_frame_bg:WaitForChild("GameName")

local hud_icon = screenGui:WaitForChild("HUDIcon")



--------------------------------------------------------
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local GameAnalyticsEvent = ReplicatedStorage:WaitForChild("GameAnalyticsEvent")
local GameAnalyticsFuncEvent = ReplicatedStorage:WaitForChild("GameAnalyticsFuncEvent")


--------------------------------------------------------------------
local place_id = game.PlaceId
local cfg 
local player = game:GetService("Players").LocalPlayer
local LocaleId = player.LocaleId

local canvans_size = scroll.CanvasSize.Y.Scale
local grid_padding = grid_layout.CellPadding
local grid_size = grid_layout.CellSize

local select_id = 0

local function LoadUI()
	
	--warn("moveGame LoadUI--->0")
	cfg = GameAnalyticsFuncEvent:InvokeServer()
	--warn("moveGame LoadUI--->1")

	if not cfg or not cfg.games then
		return
	end

	for k,v in ipairs(cfg.games)do
		if LocaleId == "zh-cn" then
			if not v.cn_show then
				continue
			end
		end
		if place_id == v.place then
			continue
		end
		local temp = item_pfb:Clone()
		temp.Icon.Image = (LocaleId == "zh-cn" and v.icon_cn or v.icon_en)
		temp.Icon.GameName.Text = (LocaleId == "zh-cn" and v.name_cn or v.name_en)
		local id = k
		temp.Icon.MouseButton1Click:Connect(function()
			ItemClick(id)
		end)
		temp.Parent = scroll
		temp.Name = k
		temp.LayoutOrder = k
	end
	--自适应
	local num = #cfg.games
	if num > 8 then
		local mul = math.ceil(num/4)
		mul = mul/2
		scroll.CanvasSize = UDim2.fromScale(0,canvans_size*mul)
		grid_layout.CellPadding = UDim2.fromScale(grid_padding.X.Scale/mul,grid_padding.Y.Scale/mul)
		grid_layout.CellSize = UDim2.fromScale(grid_size.X.Scale,grid_size.Y.Scale/mul)
	end
	
	---------------
	main_frame_title.Text = (LocaleId == "zh-cn" and "游戏中心" or "Game Center")
	show_title.Text = (LocaleId == "zh-cn" and "游戏介绍" or "Game Introduction")
	sure_btn_info.Text = (LocaleId == "zh-cn" and "进入" or "PLAY")
	cancel_btn_info.Text = (LocaleId == "zh-cn" and "关闭" or "CLOSE")
	bottom_info.Text = (LocaleId == "zh-cn" and "选择喜爱的游戏，点击开启传送门" or "Choose your favorite game,click to play it")
end



local  HttpGetConfig = coroutine.wrap(function()

	local r,e,old_timer = true,691200,os.time()
	while r do
		wait()
		if os.time() - old_timer >= e then	
			old_timer = os.time()
			for _,val in ipairs(scroll:GetChildren()) do
				if not val:IsA("UIGridLayout") then
					val:Destroy()
				end
			end
			LoadUI()
		end
	end
end)

function ItemClick(id)
	select_id = id
	show_title.Text = (LocaleId == "zh-cn" and cfg.games[id].name_cn or cfg.games[id].name_en) 
	show_name.Text = (LocaleId == "zh-cn" and cfg.games[id].name_cn or cfg.games[id].name_en)
	show_icon.Image = (LocaleId == "zh-cn" and cfg.games[id].icon_cn or cfg.games[id].icon_en)
	show_des.Text = (LocaleId == "zh-cn" and cfg.games[id].des_cn or cfg.games[id].des_en)
	show_frame.Visible = true
end



sure_btn.MouseButton1Click:Connect(function()
	if select_id ~= 0 and cfg.games[select_id].place  then
		local id = cfg.games[select_id].place
		local r,e = xpcall(function()
			tips.Text = (LocaleId == "zh-cn" and "正在传送..." or "Teleporting...")
			tips.Visible = true
			GameAnalyticsEvent:FireServer(cfg.games[select_id].name_en)
			TeleportService:Teleport(id)
			show_frame.Visible = false
		end,debug.traceback)
		if not r then
			-- warn(e)
			TeleportService:Teleport(place_id)
			tips.Text = (LocaleId == "zh-cn" and "传送错误！" or "Teleport Failed!")
			tips.Visible = false
		end
	end
end)

cancel_btn.MouseButton1Click:Connect(function()
	tips.Visible = false
	show_frame.Visible = false
end)

main_frame_close.MouseButton1Click:Connect(function()
	tips.Visible = false
	show_frame.Visible = false
	main_frame.Visible = false
end)

main_cloae_window.MouseButton1Click:Connect(function()
	tips.Visible = false
	show_frame.Visible = false
	main_frame.Visible = false
end)

show_cloae_window.MouseButton1Click:Connect(function()
	tips.Visible = false
	show_frame.Visible = false
end)

hud_icon.MouseButton1Click:Connect(function()
	tips.Visible = false
	show_frame.Visible = false
	main_frame.Visible = true
end)

LoadUI()
HttpGetConfig()